package com.ll.beautymall.utils;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ll.beautymall.R;

import java.util.List;

/**
 * Created by Administrator on 2016/10/26.
 */
public class ConfigPopupWindow {

    private PopupWindow window;
    private Context mContext;
    private ArrayAdapter popAdapter;
    private GridView gv_pop;
    private int currentNav = 0;

    public void initPopupWindow(Context context) {
        mContext = context;
        window = new PopupWindow(context);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());
    }


    public void setContentView(View contentView, int width, int height) {
        window.setContentView(contentView);
        window.setWidth(width);
        window.setHeight(height);
        gv_pop = (GridView) contentView.findViewById(R.id.gl_home_categories);
        popAdapter = new ArrayAdapter(mContext, R.layout.home_nav_open_item, R.id.tv_more_item);
        gv_pop.setAdapter(popAdapter);
    }

    public void showPopup(View anchor) {
        if (!window.isShowing() && window != null) {
            window.showAsDropDown(anchor, 0, 0);
            window.update();
//            if (window.isShowing()) {
//                //设置初始状态,报错，此时不是应该已经有数据了吗?window必须显现后才能设置状态，或者在适配器中设置
//                if (currentNav == 0) {
//                    View childAt = gv_pop.getChildAt(0);
//                    TextView tv = (TextView) childAt.findViewById(R.id.tv_more_item);
//                    tv.setTextColor(Color.parseColor("#CB0000"));
//                }
//            }
        }
    }

    public void disimssPopup() {
        if (window.isShowing() && window != null) {
            window.dismiss();
        }
    }

    public void setGvData(List<String> titles) {
        popAdapter.addAll(titles);
        popAdapter.notifyDataSetChanged();
    }

    public void setOnPopGvClickLisenter(final ViewPager vp) {
        gv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                disimssPopup();
                vp.setCurrentItem(i);
            }
        });


        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (currentNav != position) {//---->gv_pop此时并没有填充，所以获取不到View
                    View childAt = gv_pop.getChildAt(position);
                    if (childAt != null) {
                        TextView tv = (TextView) childAt.findViewById(R.id.tv_more_item);
                        tv.setTextColor(Color.parseColor("#CB0000"));

                        View pre = gv_pop.getChildAt(currentNav);
                        TextView preTv = (TextView) pre.findViewById(R.id.tv_more_item);
                        preTv.setTextColor(Color.BLACK);
                    }
                    currentNav = position;
                }
            }
        });
    }
}
